package org.pointex.common.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.pointex.common.model.PageEntity;
import org.pointex.common.model.PagingResult;
import org.pointex.dao.impl.UsersDaoImpl;
import org.pointex.domain.Users;
import org.pointex.service.impl.UserDetailsServiceImpl;
import org.pointex.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @author Szqiu
 *
 */
@Controller
public class BaseController extends ApplicationObjectSupport {

	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected ServletContext context;
	protected HttpSession session;
	protected static Users users;

	@Autowired
	protected UsersDaoImpl usersDaoImpl;

	/**
	 * 调用日志
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 
	 * @Title: feedback
	 * @Description:
	 * @param @param response
	 * @param @param content
	 * @param @throws IOException 设定文件
	 * @return void 返回类型
	 * @throws
	 */
	public void feedback(HttpServletResponse response, String content)
			throws IOException {
		response.setContentType("text/plain; charset=utf-8");
		PrintWriter writer = response.getWriter();
		writer.write(content);
		writer.flush();
		writer.close();
	}

	@ModelAttribute
	public void setReqAndRes(HttpServletRequest request,
			HttpServletResponse response) {
		this.request = request;
		this.response = response;
		this.session = request.getSession();
	}

	// 获取用户信息
	protected Users getUsersItem(HttpServletRequest request) {
		//是用户登录后才有值的，通过了JAAS认证，也就是登录,没有通过认证就没有返回值。
		System.out.println(request.getUserPrincipal().getName());
		
		return users;
	}

	public static boolean isNuOrEmpty(String... obj) {
		boolean flag = false;
		for (String o : obj) {
			if (StringUtil.isNullOrEmpty(o)) {
				flag = true;
			}
		}
		return flag;
	}

	public static final String toJsr(JSONObject json) {
		return JSON.toJSONString(json);
	}

	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}

}